<?php  
class Mtintuc extends CI_model{ 
     // đếm tổng số record trong table book 
    public function count_all(){ 
        return $this->db->count_all('tintuc'); 
    }

    public function listall(){
        $query=$this->db->get("tintuc");
        return $query->result_array();
    }
   
    function GetTinTucByTheLoai($id_theloai){
	
        $this->db->where("id_theloai",$id_theloai);
        $query=$this->db->get('tintuc');
        return $query->result_array();
    }
    function Insert($data) {
        $this->db->insert('tintuc',$data);
        return $this->db->insert_id();
    }

    public function Update($data, $TinTuc_id) {
        $this->db->where('TinTuc_id', $TinTuc_id);
        $this->db->update('tintuc', $data); 
    }

    function Delete($Tintuc_id){
        $this->db->where("TinTuc_id",$Tintuc_id);
        $this->db->delete('tintuc');
        //unlink("./upload/" . $file[0]['file_name']); //xóa file upload trong thư mục chứa
        return $this->db->affected_rows();     
    }
	 public function list_with_page($number, $offset){ 
        $this->db->order_by("TinTuc_id", "desc"); // order by ID
        $query =  $this->db->get('tintuc',$number,$offset); 
        $result_array = $query->result_array();
        $i = 0;
        foreach ($result_array as $items) {
             $result_array[$i]['subTieuDe']   = $result_array[$i]['TieuDe'];
             $result_array[$i]['subTomTat']   = $result_array[$i]['TomTat'];
             $result_array[$i]['subNoiDung']  = $result_array[$i]['NoiDung'];
             $result_array[$i]['subKeywords'] = $result_array[$i]['Keywords'];

            $i++;
        }
        return $result_array; 
    } 
	 function GetTinTucPaging($number, $offset){
        $this->db->select('*');
        $this->db->from('tintuc');
		$this->db->where('AnHien', 1);
		$this->db->order_by("TinTuc_id", "desc"); 
        $this->db->limit($number,$offset);
        $query=$this->db->get();
        return $query->result_array();
    }

    function CountRecordTinTucPaging(){
        $this->db->select('*');
        $this->db->from('tintuc');
		$this->db->where('AnHien', 1);
        $query=$this->db->get();
        return $query->num_rows();
    }
}
?>